package jhu.profiteer;

import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailUtil
{
  // send email
  public static void SendMail(String toAddress, String passcode) throws MessagingException
  {

    String mailFrom = "admin@profiteer.jhu.edu";
    String mailTo = toAddress;
    String mailSubject = "From Profiteer Account services";
    String mailMessage ="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 "
                        +"Transitional//EN\"\n\"http://www.w3.org/TR/html4/loose.dtd\">"
                        +"<html>"
                        +"  <head>"
                        +"    <title>Profiteer Account services</title>"
                        +"  </head>"
                        +"  <body bgcolor=\"#ffffcc\" style=\"margin-left: auto;margin-right: auto;width: 750px;\">"
                        +"    <div style=\"border:solid 10px black;width:750px;\">"
                        +"      <table width=\"100%\">"
                        +"        <tr>"
                        +"          <td align=\"center\">"
                        +"            <div>"
                        +"              <h3>Greetings From Profiteer</h3><br/><hr />"
                        +"            </div>"
                        +"            <div style=\"width:70%;text-align:left;\"><br />"
                        +"              We have received a request to reset your password."
                        +"              Your new password is " + passcode + "."
                        +"              Please log in with your new password and change it for security reasons."
                        +"              If you did not request your password to be reset, please contact the game administrators."
                        +"            </div>"
                        +"          </td>"
                        +"        </tr>"
                        +"      </table>"
                        +"    </div>"
                        +"  </body>"
                        +"</html>";

    try
    {
      Properties props = new Properties();
      props.put("mail.smtp.host", "smtp.johnshopkins.edu");
      Session session = Session.getDefaultInstance(props);
      Message m = new MimeMessage(session);
      m.setSubject(mailSubject);
      m.setContent(mailMessage, "text/html");
      Address from = new InternetAddress(mailFrom);
      Address to = new InternetAddress(mailTo);
      m.setFrom(from);
      m.setRecipient(Message.RecipientType.TO, to);
      Transport.send(m);
    }
    catch (MessagingException e)
    {
      throw e;
    }
  }
}

